import pygame
import random

# 初始化 Pygame
pygame.init()

# 定义常量
WIDTH, HEIGHT = 400, 600
FRUIT_RADIUS = 20
GRAVITY = 1
FPS = 60

# 定义颜色
WHITE = (255, 255, 255)

# 创建游戏窗口
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("合成大西瓜")
clock = pygame.time.Clock()

# 水果列表，按从小到大排序
fruits = ["樱桃", "草莓", "橘子", "柠檬", "苹果", "菠萝", "西瓜"]

# 水果类
class Fruit:
    def __init__(self, x, y, fruit_type):
        self.x = x
        self.y = y
        self.fruit_type = fruit_type
        self.radius = FRUIT_RADIUS * (fruits.index(fruit_type) + 1)
        self.vy = 0

    def fall(self):
        self.vy += GRAVITY
        self.y += self.vy

    def draw(self):
        pygame.draw.circle(screen, get_fruit_color(self.fruit_type), (int(self.x), int(self.y)), self.radius)


# 获取水果颜色（简单示例，可根据实际调整）
def get_fruit_color(fruit_type):
    if fruit_type == "樱桃":
        return (255, 0, 0)
    elif fruit_type == "草莓":
        return (255, 50, 50)
    elif fruit_type == "橘子":
        return (255, 165, 0)
    elif fruit_type == "柠檬":
        return (255, 255, 0)
    elif fruit_type == "苹果":
        return (0, 255, 0)
    elif fruit_type == "菠萝":
        return (255, 215, 0)
    elif fruit_type == "西瓜":
        return (0, 128, 0)


# 检查两个水果是否碰撞
def check_collision(fruit1, fruit2):
    distance = ((fruit1.x - fruit2.x) ** 2 + (fruit1.y - fruit2.y) ** 2) ** 0.5
    return distance < (fruit1.radius + fruit2.radius)


# 合并两个相同水果
def merge_fruits(fruits_list, index1, index2):
    fruit1 = fruits_list[index1]
    fruit2 = fruits_list[index2]
    if fruit1.fruit_type == fruit2.fruit_type:
        new_fruit_type = fruits[min(fruits.index(fruit1.fruit_type) + 1, len(fruits) - 1)]
        new_fruit = Fruit((fruit1.x + fruit2.x) / 2, (fruit1.y + fruit2.y) / 2, new_fruit_type)
        del fruits_list[index1]
        del fruits_list[index2 - 1]  # 因为删除了前面的元素，索引要减 1
        fruits_list.append(new_fruit)


# 主游戏循环
running = True
falling_fruit = Fruit(WIDTH // 2, 0, random.choice(fruits[:3]))
fruits_on_screen = []

while running:
    clock.tick(FPS)
    screen.fill(WHITE)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.MOUSEMOTION:
            falling_fruit.x = event.pos[0]

    # 让下落的水果下落
    falling_fruit.fall()

    # 检查是否到底部
    if falling_fruit.y + falling_fruit.radius >= HEIGHT:
        fruits_on_screen.append(falling_fruit)
        falling_fruit = Fruit(WIDTH // 2, 0, random.choice(fruits[:3]))

    # 检查与其他水果的碰撞
    for i, fruit in enumerate(fruits_on_screen):
        if check_collision(falling_fruit, fruit):
            fruits_on_screen.append(falling_fruit)
            falling_fruit = Fruit(WIDTH // 2, 0, random.choice(fruits[:3]))
            break

    # 检查已在屏幕上的水果之间的碰撞并合并
    for i in range(len(fruits_on_screen)):
        for j in range(i + 1, len(fruits_on_screen)):
            if check_collision(fruits_on_screen[i], fruits_on_screen[j]):
                merge_fruits(fruits_on_screen, i, j)

    # 绘制所有水果
    falling_fruit.draw()
    for fruit in fruits_on_screen:
        fruit.draw()

    pygame.display.flip()

pygame.quit()